home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.2 / swatch.about.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-25  |  10.9 KB  |  496 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     swatch.about.c
  4.     Copyright (c) 1990, Adobe Systems, Inc.
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Conditional Compilation Flags
  12.  **
  13.  **/
  14.  
  15.  
  16. /**-----------------------------------------------------------------------------
  17.  **
  18.  **    Headers
  19.  **
  20.  **/
  21.  
  22. #include <Sound.h>
  23.  
  24. #include "swatch.resources.h"
  25. #include "swatch.h"
  26. #include "swatch.prefs.h"
  27. #include "swatch.about.h"
  28.  
  29.  
  30. /**-----------------------------------------------------------------------------
  31.  **
  32.  ** Private Macros
  33.  **
  34.  **/
  35.  
  36.  
  37. /**-----------------------------------------------------------------------------
  38.  **
  39.  ** Private Constants
  40.  **
  41.  **/
  42.  
  43. #define MAX_FLYING            10
  44. #define TICK_FREQ            2
  45. #define CHAR_WIDTH            8
  46. #define CHAR_HEIGHT            12
  47.  
  48. #define MAX_BITS            14
  49.  
  50. #define LAUNCH_FREQ            (10)
  51. #define SWIRL_DELAY            (9*60 + 52)
  52. #define BY_DELAY            (15*60 + 30)
  53. #define BIT_DELAY            (18*60 + 40)
  54. #define INTER_BIT_DELAY        (350)
  55. #define INTER_BIT_INC        (40)
  56. #define ERASE2_DELAY        (32*60)
  57. #define FIRST_BEAT_DELAY    (30)
  58. #define CREDITS2_DELAY        (28*60)
  59. #define HEART_FRAMES        (200)
  60.  
  61.  
  62. /**-----------------------------------------------------------------------------
  63.  **
  64.  ** Private Variables
  65.  **
  66.  **/
  67.  
  68. char credits[] = "Swatch 1.2";
  69. char credits2[] = "\pCopyright ©1991, joe holt";
  70.  
  71.  
  72. typedef struct {
  73.     Boolean flying;
  74.     Fixed x, y, dx, dy;
  75.     int16 size;
  76.     Rect r;
  77. } Flying_bit;
  78.  
  79. typedef struct {
  80.     int16 stage;
  81.     char c;
  82.     uns32 next_tick;
  83.     Fixed x, y, dx, dy, ddx, ddy;
  84.     Fixed maxx, maxy, dmax;
  85.     int16 num_bits, num_bits2;
  86.     Flying_bit bit[MAX_BITS];
  87.     Rect r;
  88. } Flyer;
  89.  
  90.  
  91. /**-----------------------------------------------------------------------------
  92.  **
  93.  ** Private Functions
  94.  **
  95.  **/
  96.  
  97. int16 random( int16 max );
  98.  
  99.  
  100. /*******************************************************************************
  101.  **
  102.  **    Public Variables
  103.  **
  104.  **/
  105.  
  106.  
  107. /*******************************************************************************
  108.  **
  109.  **    Public Functions
  110.  **
  111.  **/
  112.  
  113.  
  114. /*******************************************************************************
  115.  ***
  116.  *** prototype
  117.  ***
  118.  *** summary
  119.  ***
  120.  *** History:
  121.  ***
  122.  *** To Do:
  123.  ***
  124.  ***/
  125.  
  126. void Do_about( WindowPtr the_window )
  127. {
  128.     Flyer *fly;
  129.     register Flyer *f;
  130.     register Flying_bit *b;
  131.     register int16 i, j;
  132.     int16 launch_c;
  133.     uns32 next_launch, ticker, start_ticks, swirl_delay;
  134.     int16 num_active;
  135.     Fixed window_right, window_bottom, sx, sy, byx, byy;
  136.     Boolean got_beat;
  137.     int16 inter_bit_delay;
  138.     Handle snd;
  139.     SndChannelPtr chan;
  140.  
  141.     fly = (Flyer *)NewPtr( MAX_FLYING * sizeof( Flyer ) );
  142.     if ( !fly )
  143.         return;
  144.  
  145.     ClipRect( &the_window->portRect );
  146.     snd = Read_riff();
  147.  
  148.     HideCursor();
  149.     set_back_color( HEAP_UNLOCKED_COLOR );
  150.     set_fore_color( HEAP_LOCKED_COLOR );
  151.  
  152.     EraseRect( &the_window->portRect );
  153.     window_right = (int32) the_window->portRect.right << 16;
  154.     window_bottom = (int32) the_window->portRect.bottom << 16;
  155.  
  156.     for ( f = fly, i = MAX_FLYING; i; --i, ++f ) {
  157.         Fixed n, ni, sp;
  158.  
  159.         f->stage = 0;
  160.         f->num_bits2 = f->num_bits = random( MAX_BITS / 2 ) + MAX_BITS / 2;
  161.         n = FixDiv( (int32) random( 360 ) << 16, 0x00394FEF /* 2pi / 360 */ );
  162.         ni = FixDiv( 0x0006487F /* 2pi */, (int32) f->num_bits << 16 );
  163.         for (  b = f->bit, j = f->num_bits; j; --j, ++b ) {
  164.             b->flying = TRUE;
  165.             SetRect( &b->r, 0, 0, 0, 0 );
  166.             b->size = random( 10 ) + 5;
  167.             sp = (int32) b->size << 16;
  168.             b->size = (15 - b->size) / 5 + 1;
  169.             b->dx = FixMul( Frac2Fix( FracCos( n ) ), sp );
  170.             b->dy = FixMul( Frac2Fix( FracSin( n ) ), sp );
  171.             n += ni;
  172.         }
  173.     }
  174.  
  175.     if ( snd ) {
  176.         chan = NULL;
  177.         if ( !SndNewChannel( &chan, 0, 0, NULL ) )
  178.             SndPlay( chan, snd, TRUE );
  179.     }
  180.     start_ticks = TickCount();
  181.  
  182.     sx = FixDiv( window_right, 2L << 16 ) + (21L << 16);
  183.     sy = FixDiv( window_bottom, 2L << 16 ) - (165L << 16);
  184.     byx = FixDiv( window_right, 2L << 16 ) - (65L << 16);
  185.     byy = FixDiv( window_bottom, 2L << 16 ) + (19L << 16);
  186.     swirl_delay = SWIRL_DELAY;
  187.     if ( sy < 0 )
  188.         swirl_delay = swirl_delay - ( FixMul( (175L << 16) + sy, 0x00002800 ) >> 16 );
  189.     next_launch = start_ticks + swirl_delay;
  190.     got_beat = FALSE;
  191.     launch_c = 0;
  192.     num_active = MAX_FLYING;
  193.     inter_bit_delay = 1;
  194.     while ( !Button() && num_active ) {
  195.         for ( f = fly, i = MAX_FLYING; i; --i, ++f ) {
  196.  
  197.             switch ( f->stage ) {
  198.             case 0:
  199.                 if ( got_beat ) {
  200.                     f->stage = 2;
  201.                     continue;
  202.                 }
  203.                 if ( TickCount() < start_ticks + FIRST_BEAT_DELAY )
  204.                     continue;
  205.  
  206.                 got_beat = TRUE;
  207.                 f->r.top = -40;
  208.                 f->r.bottom = -40 + CELL_HEIGHT * 2;
  209.                 f->r.left = the_window->portRect.right / 2 - 121;
  210.                 f->r.right = f->r.left + 242;
  211.                 f->stage++;
  212.                 f->next_tick = 0;
  213.                 break;
  214.  
  215.             case 1: {
  216.                 Rect r;
  217.  
  218.                 if ( TickCount() < f->next_tick )
  219.                     continue;
  220.  
  221.                 f->next_tick = TickCount() + TICK_FREQ * 2;
  222.                 ScrollRect( &f->r, 0, 1, the_window->clipRgn );
  223.                 OffsetRect( &f->r, 0, 1 );
  224.  
  225.                 if ( f->r.top < 1 ) {
  226.                     r = f->r;
  227.                     set_fore_color_or_pattern( BLACK_COLOR );
  228.                     MoveTo( r.left + 20, r.top + CELL_HEIGHT - 2 );
  229.                     DrawString( (StringPtr) "\pLocked" );
  230.                     MoveTo( r.left + 100, r.top + CELL_HEIGHT - 2 );
  231.                     DrawString( (StringPtr) "\pUnlocked" );
  232.                     MoveTo( r.left + 180, r.top + CELL_HEIGHT - 2 );
  233.                     DrawString( (StringPtr) "\pFree" );
  234.  
  235.                     r.top += CELL_HEIGHT + 2;
  236.                     r.bottom -= 2;
  237.                     set_fore_color_or_pattern( HEAP_BORDER_COLOR );
  238.                     FrameRect( &r );
  239.                     InsetRect( &r, 1, 1 );
  240.     
  241.                     r.left = r.right - 80;
  242.                     set_fore_color_or_pattern( HEAP_FREE_COLOR );
  243.                     PaintRect( &r );
  244.                     r.left -= 80;
  245.                     r.right -= 80;
  246.                     set_fore_color_or_pattern( HEAP_UNLOCKED_COLOR );
  247.                     PaintRect( &r );
  248.                     r.left -= 80;
  249.                     r.right -= 80;
  250.                     set_fore_color_or_pattern( HEAP_LOCKED_COLOR );
  251.                     PaintRect( &r );
  252.                 }
  253.                 else {
  254.                     RgnHandle aRgn;
  255.                     
  256.                     ClipRect( &the_window->portRect );
  257.                     aRgn = NewRgn();
  258.                     r = f->r;
  259.                     r.top += CELL_HEIGHT + 2;
  260.                     r.bottom -= 2;
  261.                     RectRgn( aRgn, &r );
  262.                     DiffRgn( the_window->clipRgn, aRgn, the_window->clipRgn );
  263.  
  264.                     r.top = f->r.top + 5;
  265.                     r.bottom = r.top + CHAR_HEIGHT;
  266.                     r.left = f->r.left + 20;
  267.                     r.right = r.left + StringWidth( (StringPtr) "\pLocked" );
  268.                     RectRgn( aRgn, &r );
  269.                     DiffRgn( the_window->clipRgn, aRgn, the_window->clipRgn );
  270.  
  271.                     r.left = f->r.left + 100;
  272.                     r.right = r.left + StringWidth( (StringPtr) "\pUnlocked" );
  273.                     RectRgn( aRgn, &r );
  274.                     DiffRgn( the_window->clipRgn, aRgn, the_window->clipRgn );
  275.  
  276.                     r.left = f->r.left + 180;
  277.                     r.right = r.left + StringWidth( (StringPtr) "\pFree" );
  278.                     RectRgn( aRgn, &r );
  279.                     DiffRgn( the_window->clipRgn, aRgn, the_window->clipRgn );
  280.  
  281.                     DisposeRgn( aRgn );
  282.                     f->stage++;
  283.                     continue;
  284.                 }
  285.                 ClipRect( &the_window->portRect );
  286.                 break;
  287.             }
  288.  
  289.             case 2:
  290.                 if ( TickCount() < next_launch )
  291.                     continue;
  292.  
  293.                 f->stage++;
  294.                 if ( launch_c < 20 ) {
  295.                     f->maxx = 18L << 16;
  296.                     f->maxy = 18L << 16;
  297.                     f->dx = -f->maxx;
  298.                     f->dy = 0;
  299.                     f->x = sx;
  300.                     f->y = sy;
  301.                     f->ddx = -1L << 16;
  302.                     f->ddy = 1L << 16;
  303.                     f->dmax = 0x1DFF;
  304.                     sx += (int32) CHAR_WIDTH << 16;
  305.                     f->next_tick = 0;
  306.                 }
  307.                 else {
  308.                     f->maxx = 0;
  309.                     f->maxy = 0;
  310.                     f->dx = 0;
  311.                     f->dy = 0;
  312.                     f->x = byx;
  313.                     f->y = byy;
  314.                     f->ddx = 0;
  315.                     f->ddy = 0;
  316.                     f->dmax = 0;
  317.                     byx += (int32) CHAR_WIDTH << 16;
  318.                     f->next_tick = start_ticks + BY_DELAY + random( 100 );
  319.                 }
  320.                 f->c = credits[launch_c++];
  321.                 SetRect( &f->r, 0, 0, 0, 0 );
  322.                 next_launch = TickCount() + LAUNCH_FREQ;
  323.                 break;
  324.  
  325.             case 3:
  326.                 if ( TickCount() < f->next_tick )
  327.                     continue;
  328.  
  329.                 f->next_tick = TickCount() + TICK_FREQ;
  330.     
  331.                 f->x += f->dx;
  332.                 f->y += f->dy;
  333.                 f->dx += f->ddx;
  334.                 if ( f->ddx > 0 && f->dx >= f->maxx ) {
  335.                     f->ddx = -f->ddx;
  336.                     f->dx = f->maxx;
  337.                 }
  338.                 else if (f->ddx < 0 && f->dx <= -f->maxx) {
  339.                     f->ddx = -f->ddx;
  340.                     f->dx = -f->maxx;
  341.                 }
  342.                 f->dy += f->ddy;
  343.                 if ( f->ddy > 0 && f->dy >= f->maxy ) {
  344.                     f->ddy = -f->ddy;
  345.                     f->dy = f->maxy;
  346.                 }
  347.                 else if (f->ddy < 0 && f->dy <= -f->maxy) {
  348.                     f->ddy = -f->ddy;
  349.                     f->dy = -f->maxy;
  350.                 }
  351.                 f->maxx -= f->dmax;
  352.                 f->maxy -= f->dmax;
  353.             
  354.                 EraseRect( &f->r );
  355.                 f->r.left = f->x >> 16;
  356.                 f->r.bottom = (f->y >> 16) + 2;
  357.                 f->r.right = f->r.left + CHAR_WIDTH;
  358.                 f->r.top = f->r.bottom - CHAR_HEIGHT;
  359.         
  360.                 MoveTo( f->r.left, f->r.bottom - 2 );
  361.                 DrawChar( f->c );
  362.  
  363.                 if ( f->maxx <= 0 || f->maxy <= 0 ) {
  364.                     f->stage++;
  365.                     for ( j = f->num_bits, b = f->bit; j; --j, ++b ) {
  366.                         b->x = f->x + (CHAR_WIDTH / 2 << 16);
  367.                         b->y = f->y - (CHAR_HEIGHT / 2 << 16);
  368.                     }
  369.                 }
  370.                 break;
  371.  
  372.             case 4:
  373.                 if ( TickCount() < start_ticks + BIT_DELAY )
  374.                     continue;
  375.  
  376.                 f->next_tick = TickCount() + random( inter_bit_delay );
  377.                 if ( inter_bit_delay < INTER_BIT_DELAY )
  378.                     inter_bit_delay += INTER_BIT_INC;
  379.                 f->stage++;
  380.             /* fall into next */
  381.             case 5:
  382.                 if ( TickCount() < f->next_tick )
  383.                     continue;
  384.  
  385.                 f->r.left = f->x >> 16;
  386.                 f->r.bottom = (f->y >> 16) + 2;
  387.                 f->r.right = f->r.left + CHAR_WIDTH;
  388.                 f->r.top = f->r.bottom - CHAR_HEIGHT;
  389.                 EraseRect( &f->r );
  390.                 f->stage++;
  391.             /* fall into next */
  392.             case 6:
  393.                 if ( TickCount() < f->next_tick )
  394.                     continue;
  395.  
  396.                 for ( j = f->num_bits, b = f->bit; j; --j, ++b ) {
  397.                     if ( b->flying ) {
  398.                         EraseRect( &b->r );
  399.     
  400.                         b->x += b->dx;
  401.                         b->y += b->dy;
  402.                         if ( b->x < 0 || b->x > window_right ||
  403.                                 b->y < 0 || b->y > window_bottom ) {
  404.                             b->flying = FALSE;
  405.                             if ( !--f->num_bits2 ) {
  406.                                 f->stage++;
  407.                                 f->next_tick = start_ticks + CREDITS2_DELAY;
  408.                                 continue;
  409.                             }
  410.                         }
  411.                         else {
  412.                             b->r.left = b->x >> 16;
  413.                             b->r.right = b->r.left + b->size;
  414.                             b->r.top = b->y >> 16;
  415.                             b->r.bottom = b->r.top + b->size;
  416.                             PaintRect( &b->r );
  417.                         }
  418.                     }
  419.                 }
  420.                 f->next_tick = TickCount() + 2;
  421.                 break;
  422.  
  423.             case 7:
  424.                 if ( TickCount() >= start_ticks + ERASE2_DELAY ) {
  425.                     f->stage++;
  426.                     --num_active;
  427.                     continue;
  428.                 }
  429.  
  430.                 if ( TickCount() < f->next_tick )
  431.                     continue;
  432.                 MoveTo( ( the_window->portRect.right - StringWidth( (StringPtr) credits2 ) ) / 2,
  433.                         the_window->portRect.bottom - 20 );
  434.                 DrawString( (StringPtr) credits2 );
  435.                 f->next_tick = TickCount() + ERASE2_DELAY; /* never again */
  436.                 break;
  437.  
  438.             default:
  439.                 break;
  440.             }
  441.         }
  442.  
  443.         
  444.     }
  445.  
  446.     ticker = start_ticks + ERASE2_DELAY;
  447.     while ( !Button() && TickCount() < ticker );
  448.  
  449.     ClipRect( &the_window->portRect );
  450.  
  451.     if ( Use_color ) {
  452.         Rect r;
  453.  
  454.         set_back_color( BACK_COLOR );
  455.         set_fore_color( BLACK_COLOR );
  456.  
  457.         r = the_window->portRect;
  458.         while ( r.top < the_window->portRect.bottom ) {
  459.             r.bottom = r.top + CELL_HEIGHT;
  460.             EraseRect( &r );
  461.             r.top += CELL_HEIGHT;
  462.             ticker = TickCount() + 3;
  463.             while ( TickCount() < ticker );
  464.         }
  465.     }
  466.     else
  467.         EraseRect( &the_window->portRect );
  468.     InvalRect( &the_window->portRect );
  469.  
  470.     DisposPtr( (Ptr) fly );
  471.     if ( snd ) {
  472.         SndDisposeChannel( chan, TRUE );
  473.         Dispose_riff( snd );
  474.     }
  475.     FlushEvents( mUpMask | mDownMask | keyDownMask | autoKeyMask | keyUpMask, 0 );
  476.     ShowCursor();
  477. }
  478.  
  479.  
  480. /**-----------------------------------------------------------------------------
  481.  **
  482.  **    Private Functions
  483.  **
  484.  **/
  485.  
  486.  
  487. int16 random( int16 max )
  488. {
  489.     int16 x;
  490.  
  491.     x = Random();
  492.     if ( x < 0 )
  493.         x = -x;
  494.     return x % max;
  495. }
  496.